import siteinfo from "../siteinfo.js"; // 工具类

const api_root = siteinfo.siteroot

// 登录方法
export function MPLOGIN() {
	return new Promise((resolve, reject) => {
		uni.login({
			// #ifdef MP-WEIXIN
			provider: 'weixin',
			// #endif
			success: (res) => {
				let {
					code,
					anonymousCode
				} = res
				let header = {}
				// #ifdef MP-WEIXIN
				header.ThirdType = 'WECHAT_MINI_PROGRAM'
				// #endif

				// #ifdef MP-TOUTIAO
				header.ThirdType = 'TIKTOK_MINI_PROGRAM'
				// #endif

				header.version = "lan"
				let data = {
					code,
					anonymousCode
				}
				uni.request({
					url: api_root + '/app-api/member/auth/mini-program-login',
					data: data,
					method: 'POST', // POST请求
					header: header,
					success: (result) => {
						if (result.data.code == 0) {
							let {
								accessToken,
								userId,
								nickName
							} = result.data.data

							uni.setStorageSync("token", accessToken)
							// uni.setStorageSync("token", '81aba18689984af285301f20829b1c07')
							uni.setStorageSync("userInfo", {
								userId,
								nickName
							})
							resolve(result.data)
						}
					},
					fail(err) {
						// uni.setStorageSync("token", '81aba18689984af285301f20829b1c07')
						console.log(err);
					}
				})
			}
		})
	})
}